package start.spring.basic.interceptor;

import java.io.IOException;
import java.lang.reflect.Type;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;

/**
 * 
 * ****************************************************************************
 * fastjson转换使用string方式序列化
 *
 * @author(作者)：xuyongyun	
 * @date(创建日期)：2023年2月21日
 ******************************************************************************
 */
public class ToStringSerializer implements ObjectSerializer {

	public static final ToStringSerializer instance = new ToStringSerializer();

    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
                      int features) throws IOException {
        SerializeWriter out = serializer.out;

        if (object == null) {
            out.writeNull();
            return;
        }

        String strVal = object.toString();
        out.writeString(strVal);
    }
}
